package com.bdk.middle.management.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.common.status.ErrorCode;
import com.bdk.middle.management.api.domain.AdminDO;
import com.bdk.middle.management.api.domain.RoleDO;
import com.bdk.middle.management.api.domain.RoleDO2;
import com.bdk.middle.management.api.query.AdminQueryDO;
import com.bdk.middle.management.api.query.RoleQueryDO;
import com.bdk.middle.management.service.AdminService;
import com.bdk.middle.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-01-23
 */
@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody RoleDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = roleService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody RoleDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = roleService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }


    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody RoleDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = roleService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody RoleDO domain) {

        ObjectResponse<RoleDO> response = new ObjectResponse();

        ResultDO result = roleService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((RoleDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    /**
     * 查用户相关角色
     * @return
     */
    @RequestMapping(value="/listOfUser.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse listOfUser(@RequestBody AdminQueryDO domian) {

        ObjectResponse<List<RoleDO2>> response = new ObjectResponse();
        String username=domian.getUsername();
        if(username==null || StringUtils.isEmpty(username)){
            response.setStatus(ErrorCode.SYSTEM_INCORRECT_PARAM.getCode());
            response.setMsg(ErrorCode.SYSTEM_INCORRECT_PARAM.getMsg());
        }
        ResultDO result = roleService.getRoleListByUsername(username);
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((List<RoleDO2>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    /**
     * 查询所有角色,限制为超级管理员
     * @return
     */
    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list() {

        ObjectResponse<Object> response = new ObjectResponse();

        //查角色列表
        ResultDO result = roleService.getAllRole();
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

